from django.utils import timezone
from login.models import CustomToken
from django.http import JsonResponse
from utils.crypto import DECRYPT


def validate_custom_token(token_key):
    print(token_key, 'token')
    """
    验证自定义token是否正确的完整流程
    """
    try:
        # 1. 检查token是否存在
        token = CustomToken.objects.get(key=token_key)
        # 2. 检查token是否过期
        if token.expired_at < timezone.now():
            print(token_key, '1token')
            return None, "Token expired"
        # 3. 检查关联用户是否有效
        if not token.user.is_active:
            print(token_key, '2token')
            return None, "User inactive"
        # 所有验证通过
        return token.user, None

    except CustomToken.DoesNotExist:
        return None, "Invalid token"